package com.unohacha.qqj.fzdr.main;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qpg.superhttp.SuperHttp;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 求货运   R.layout.item_material_info
 */
public class TransportActivity extends BaseActivity implements OnRefreshLoadmoreListener {
    
    @BindView(R.id.common_cancle)
    ImageView commonCancle;
    @BindView(R.id.edt_seach)
    EditText edtSeach;
    @BindView(R.id.common_right_search)
    TextView commonRightSearch;
    @BindView(R.id.search_ll)
    LinearLayout searchLl;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    
    private int page = 1;
    private String content = "";
    private String la;
    private String lon;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transport;
    }

    @Override
    protected void initView() {
        super.initView();
        initRefreshLayout();

        la = Prefer.getInstance().obtainLocation(this).substring(0, Prefer.getInstance().obtainLocation(this).indexOf(","));
        lon = Prefer.getInstance().obtainLocation(this).substring(
                Prefer.getInstance().obtainLocation(this).indexOf(",") + 1,
                Prefer.getInstance().obtainLocation(this).length());
        
    }

    @Override
    protected void onResume() {
        super.onResume();
        getFreightList(content,la,lon);//求货运列表
    }

    private void initRefreshLayout(){
        mRefreshLayout.setOnRefreshLoadmoreListener(this);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
    }
    
    @OnClick({ R.id.common_cancle, R.id.common_right_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.common_cancle://返回
                finish();
                break;
            case R.id.common_right_search://搜索按钮
                content = edtSeach.getText().toString().trim();
                if(TextUtils.isEmpty(content)){
                    ToastUtils.showT("请输入搜索内容");
                }else{
                    getFreightList(content,la,lon);
                }
                
                break;
        }
    }
    
    //得到求货运列表
    private void getFreightList(String content,String la,String lon){
//        SuperHttp.post("api/myapplication/appLogisticsSeekFreight")
//                .addParam("route_start","")
//                .addParam("route_end","")
//                .addParam("long",lon)
//                .addParam("lat",la)
//                .addParam("keyword","")
//                .addParam("page",page+"")
//                .request();
    }
    
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page ++;
            }
        },2000);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1;
            }
        },2000);
    } 
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

 
}
